//
//  FQDFeedCellTitleBarView.m
//  FakeQDaily
//
//  Created by mewxiv on 8/15/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDFeedCellTitleBarView.h"

#import "Masonry.h"

@implementation FQDFeedCellTitleBarView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
        [self prepareConstraints];
    }
    return self;
}

- (void)prepareUI
{
    UIImage *iconImage = [UIImage imageNamed:@"mySubscribeIcon"];
    _iconView = [[UIImageView alloc] initWithImage:iconImage];
    _iconView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:_iconView];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.text = @"好奇心研究所";
    _titleLabel.font = FQDBoldFontWithSize(17.0f);
    _titleLabel.backgroundColor = [UIColor whiteColor];
    _titleLabel.clipsToBounds = YES;
    [self addSubview:_titleLabel];
    
    _titleBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_titleBarButton setTitle:@"" forState:UIControlStateNormal];
    [self addSubview:_titleBarButton];
    
    UIImage *followImage = [UIImage imageNamed:@"feedFollow"];
    _followButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_followButton setImage:followImage forState:UIControlStateNormal];
    [self addSubview:_followButton];
    
    UIImage *shareImage = [UIImage imageNamed:@"feedShare"];
    _shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_shareButton setImage:shareImage forState:UIControlStateNormal];
    [self addSubview:_shareButton];
    [_shareButton addTarget:self
                     action:@selector(shareFeed:)
           forControlEvents:UIControlEventTouchUpInside];
}

- (void)prepareConstraints
{
    WeakSelf;
    
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@20);
        make.left.equalTo(weakSelf.mas_left).offset(10);
        make.centerY.equalTo(weakSelf);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.iconView.mas_right).offset(8);
        make.centerY.equalTo(weakSelf.iconView);
        make.right.lessThanOrEqualTo(weakSelf.followButton.mas_left).offset(-10);
        make.height.equalTo(weakSelf);
    }];
    
    [_titleBarButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.equalTo(weakSelf);
        make.height.equalTo(weakSelf);
        make.right.equalTo(weakSelf.followButton.mas_left);
    }];
    
    [_followButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@20);
        make.right.equalTo(weakSelf.shareButton.mas_left).offset(-10);
        make.centerY.equalTo(weakSelf);
    }];
    
    [_shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@17);
        make.height.equalTo(@20);
        make.right.equalTo(weakSelf).offset(-10);
        make.centerY.equalTo(weakSelf);
    }];
}

- (void)shareFeed:(id)sender
{
    
}

- (void)setFollowed:(BOOL)followed
{
    _followed = followed;
    
    if (followed) {
        UIImage *followedImage = [UIImage imageNamed:@"feedFollowed"];
        [self.followButton setImage:followedImage forState:UIControlStateNormal];
    } else {
        UIImage *followImage = [UIImage imageNamed:@"feedFollow"];
        [self.followButton setImage:followImage forState:UIControlStateNormal];
    }
}

@end
